package com.example.ms.listener;

import com.example.ms.dto.MarketingDTO;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class SpringRabbitListener {
    // 利用RabbitListener来声明要监听的队列信息
    // 将来一旦监听的队列中有了消息，就会推送给当前服务，调用当前方法，处理消息。
    // 可以看到方法体中接收的就是消息体的内容
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "whopxx.direct", type = ExchangeTypes.DIRECT),
            key = {"0"}
    ))
    public void listenDirectQueue1(MarketingDTO msg) {
//        throw new RuntimeException("模拟抛出异常");
        System.out.println("消费者1接收到direct.queue1的消息，模拟发送短信：【" + msg + "】");
    }

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "whopxx.direct", type = ExchangeTypes.DIRECT),
            key = {"1"}
    ))
    public void listenDirectQueue2(MarketingDTO msg) {
        System.out.println("消费者2接收到direct.queue2的消息，模拟发送邮箱：【" + msg + "】");
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue1", durable = "true"),
            exchange = @Exchange(name = "delay.direct", delayed = "true"),
            key = "0"
    ))
    public void listenDelayMessage1(MarketingDTO msg) {
        System.out.println("消费者1接收到delay.queue1的消息，模拟发送短信：【" + msg + "】");
    }


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "delay.queue2", durable = "true"),
            exchange = @Exchange(name = "delay.direct", delayed = "true"),
            key = "1"
    ))
    public void listenDelayMessage2(MarketingDTO msg) {
        System.out.println("消费者2接收到delay.queue2的消息，模拟发送邮箱：【" + msg + "】");
    }
}